package com.shengqi.xinnongren.work.ui.user.activities;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectChangeListener;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.shengqi.xinnongren.R;
import com.shengqi.xinnongren.common.base.BaseMvpActivity;
import com.shengqi.xinnongren.common.config.user.UserManager;
import com.shengqi.xinnongren.common.md5.SafeUtils;
import com.shengqi.xinnongren.common.utils.KeyBoardUtils;
import com.shengqi.xinnongren.common.utils.LogUtil;
import com.shengqi.xinnongren.common.utils.ToastHelper;
import com.shengqi.xinnongren.work.dialog.AddressDialog;
import com.shengqi.xinnongren.work.model.HttpRespond;
import com.shengqi.xinnongren.work.ui.user.model.AddressBean;
import com.shengqi.xinnongren.work.ui.user.model.AddressDialogBean;
import com.shengqi.xinnongren.work.ui.user.model.ProvinceCityBean;
import com.shengqi.xinnongren.work.ui.user.presenter.AddAddressPresenter;
import com.shengqi.xinnongren.work.ui.user.view.AddAddressView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by LuoYaWei on 2018/6/25.
 */
public class AddAddressActivity extends BaseMvpActivity<AddAddressView, AddAddressPresenter> implements AddAddressView {

    private static final String EXTRA_TYPE = "type";
    private static final String EXTRA_CONTENT = "content";

    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.tv_addAddress)
    TextView tv_addAddress;

    @BindView(R.id.edt_name)
    EditText edt_name;
    @BindView(R.id.edt_phone)
    EditText edt_phone;
    @BindView(R.id.tv_address)
    TextView tv_address;
    @BindView(R.id.edt_address_info)
    EditText edt_address_info;
    @BindView(R.id.cb_default)
    CheckBox cb_default;

    private int type; //0新增 1 编辑
    private AddressBean mBean;

    private String addressId;//地址ID 编辑使用

    private String provinceId, cityId, countyId;

    public static void start(Context context, int type, AddressBean bean) {
        Intent intent = new Intent(context, AddAddressActivity.class);
        intent.putExtra(EXTRA_TYPE, type);
        intent.putExtra(EXTRA_CONTENT, bean);
        context.startActivity(intent);
    }

    @Override
    protected AddAddressPresenter createPresenter() {
        return new AddAddressPresenter();
    }

    @Override
    protected int setContentLayoutRes() {
        return R.layout.activity_add_address;
    }

    @Override
    protected void initView() {
        Intent intent = getIntent();
        if (intent != null) {
            type = intent.getIntExtra(EXTRA_TYPE, 0);
        }
        if (type == 1) {
            mBean = (AddressBean) intent.getSerializableExtra(EXTRA_CONTENT);
            edt_name.setText(mBean.name);
            edt_phone.setText(mBean.mobile);
            tv_address.setText(mBean.province + mBean.city + mBean.county);
            edt_address_info.setText(mBean.address);
            cb_default.setChecked(mBean.isDefault == 1);
            addressId = mBean.addressId;

            tv_title.setText("编辑地址");
            tv_addAddress.setText("确认");
        }

        mPresenter.getProvinceCityList();
    }

    /**
     * 选择小区
     */
    @OnClick(R.id.ll_address)
    void chooseAddress() {
        //获取省市区
        if (provinceBeans.size() > 0) {
            // 关闭键盘
            KeyBoardUtils.closeKeybord(edt_phone, this);
            pvOptions.show();
        } else {
            mPresenter.getProvinceCityList();
        }
    }

    /**
     * 确认添加
     */
    @OnClick({R.id.tv_addAddress})
    void confirmAdd() {
        String edtName = edt_name.getText().toString();
        if (TextUtils.isEmpty(edtName)) {
            ToastHelper.getInstance(this).showWarn("请输入姓名");
            return;
        }
        String edtPhone = edt_phone.getText().toString();
        if (TextUtils.isEmpty(edtPhone)) {
            ToastHelper.getInstance(this).showWarn("请输入手机号");
            return;
        }
        if (edt_phone.length() != 11) {
            ToastHelper.getInstance(this).showWarn("手机号长度不合法");
            return;
        }
        if (type == 0) {
            if (TextUtils.isEmpty(provinceId)) {
                ToastHelper.getInstance(this).showWarn("请选择省市区");
                return;
            }
        }
        String edtAddressInfo = edt_address_info.getText().toString();
        if (TextUtils.isEmpty(edtAddressInfo)) {
            ToastHelper.getInstance(this).showWarn("请输入详细地址");
            return;
        }

        String idDef = cb_default.isChecked() ? "1" : "0";
        if (type == 0) {
            mPresenter.addAddress(edtName, edtPhone, provinceId, cityId, countyId, edtAddressInfo, idDef);
        }
        if (type == 1) {
            mPresenter.editAddress(addressId, edtName, edtPhone, provinceId, cityId, countyId, edtAddressInfo, idDef);
        }
    }

    @Override
    protected void initData() {
        provinceBeans = new ArrayList<>();
        pvOptions = new OptionsPickerBuilder(this, (options1, options2, options3, v) -> {
            ProvinceCityBean provinceBean = provinceBeans.get(options1);
            ProvinceCityBean.CityListBean cityBean = cityBeans.get(options1).get(options2);
            ProvinceCityBean.CityListBean.CountListBean areaBean = areaBeans.get(options1).get(options2).get(options3);
            tv_address.setText(provinceBean.getName() + cityBean.getName() + areaBean.getName());
            provinceId = provinceBean.getAreaId();
            cityId = cityBean.getAreaId();
            countyId = areaBean.getAreaId();
        }).build();

    }

    @Override
    public void showLoadingView() {
        showLoadingDialog();
    }

    @Override
    public void hideLoadingView() {
        hideLoadingDialog();
    }

    @Override
    public void onNetworkError() {
        hideLoadingDialog();
    }

    /**
     * 添加地址成功回调
     *
     * @param respond
     */
    @Override
    public void showAddAddressRes(HttpRespond<String> respond) {
        ToastHelper.getInstance(this).showSucceed(respond.message);
        finish();
    }

    /**
     * 编辑地址成功回调
     *
     * @param respond
     */
    @Override
    public void showEditAddressRes(HttpRespond<String> respond) {
        ToastHelper.getInstance(this).showSucceed(respond.message);
        finish();
    }

    OptionsPickerView pvOptions;
    private List<ProvinceCityBean> provinceBeans;
    private List<List<ProvinceCityBean.CityListBean>> cityBeans;
    private List<List<List<ProvinceCityBean.CityListBean.CountListBean>>> areaBeans;

    /**
     * 省市区列表回调
     *
     * @param list
     */
    @Override
    public void showProvinceCityList(List<ProvinceCityBean> list) {
        provinceBeans.addAll(list);
        // 构造省市区list
        cityBeans = new ArrayList<>();
        for (int i = 0; i < provinceBeans.size(); i++) {
            cityBeans.add(list.get(i).getCityList());
        }
        areaBeans = new ArrayList<>();
        for (int i = 0; i < cityBeans.size(); i++) {
            List<ProvinceCityBean.CityListBean> cl = cityBeans.get(i);
            List<List<ProvinceCityBean.CityListBean.CountListBean>> all = new ArrayList<>();
            for (int j = 0; j < cl.size(); j++) {
                ProvinceCityBean.CityListBean city = cl.get(j);
                List<ProvinceCityBean.CityListBean.CountListBean> al = city.getCountList();
                all.add(al);
            }
            areaBeans.add(all);
        }
        pvOptions.setPicker(provinceBeans, cityBeans, areaBeans);
    }
}
